jQuery(function() {
	jQuery.ajaxSetup({
		case: false,
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		}
	});

	var doc = document,
		$doc = $(doc),
		example = {
			title: false,
			desc: false,
			link: false
		},
		base = {
			realName: false,
			phone: false,
			email: false,
			id: false,
			contact: false,
			qq: false
		},
		team = {
			type: 0,
			name: false,
			desc: false,
			pDesc: false,
			trade: false,
			peoples: false,
			plan: false,
			service: false
		};

	/*添加案例 -start-*/
	$doc.on("click", "#postInfo", function() {
		noty({text:'申请成功，请耐心等待wike工作人员审核',type:'success',timeout:500,
			callback:{
				afterClose:function(){
					window.location.href=Home;
				}
			}
		});
	});

	!function init() {
		//显示错误信息
		function showErrorMessage(node) {
			utils.errorMessage(node, "*必填");
		}
		//清除错误信息
		function clearMessage(input, node) {
			$doc.on("focus", input, function() {
				utils.clearErrMessage(node);
			});
		}
		//切换填写的表单
		function replaceTable(table, index) {
			var tableBox = $(".tableBox > div");
			table.slideUp();
			tableBox.eq(index).fadeIn();
		}

		/*验证姓名等 start*/
		$doc.on("click", ".first button", function() {
			var realName = $(".first .real_name input").val(),
				contact = $(".first .contact input").val(),
				qq = $(".first .qq input").val(),
				email = $(".first .email input").val(),
				id = $(".first .id input").val();

			if (!utils.empty(realName)) {
				base.realName = true;
			} else {
				base.realName = false;
				showErrorMessage($(".realName_error "));
			}

			if (!utils.empty(contact)) {
				base.contact = true;
			} else {
				base.contact = false;
				showErrorMessage($(".contact_error"));
			}

			if (utils.checkMoblie($(".first .contact input"))) {
				base.contact = true;
			} else {
				base.contact = false;
				alert("联系电话格式不正确！");
			}

			if (!utils.empty(qq)) {
				base.qq = true;
			} else {
				base.qq = false;
				showErrorMessage($(".qq_error"));
			}

			if (!utils.empty(id)) {
				base.id = true;
			} else {
				base.id = false;
				showErrorMessage($(".id_error"));
			}

			if (utils.isNumber(qq)) {
				base.qq = true;
			} else {
				base.qq = false;
				alert("QQ格式不正确！");
			}

			if (utils.checkQQ(qq)) {
				base.qq = true;
			} else {
				base.qq = false;
				alert("QQ格式不正确！");
			}

			if (!utils.empty(email)) {
				base.email = true;
			} else {
				base.email = false;
				showErrorMessage($(".email_error "));
			}

			if (utils.checkEmail($(".first .email input"))) {
				base.email = true;
			} else {
				base.email = false;
				alert("email格式不正确！");
			}

			if (utils.checkId($(".first .id input"))) {
				base.id = true;
			} else {
				base.id = false;
				alert("身份证格式不正确！");
			}

			if (base.realName &&
				base.contact &&
				base.qq &&
				base.email &&
				base.id) {
				console.log(1);
				//切换填写的表单
				setFacInfo();

				//do something
			}

		});

		//验证真实姓名
		$('.first .real_name input').blur(function(){
			var realname = $(".first .real_name input").val();
			if(realname !== ''){
				base.realName = true;
				$(".first .real_name input").css('border-color','#ccc');
			}else{
				base.realName = false;
				$(".first .real_name input").css('border-color','red');
			}
		});

		//鼠标移出验证手机号
		$(".first .contact input").blur(function(){
			var phoneMatch = $(".first .contact input").val().match(/^1\d{10}$/);
			if(!phoneMatch){
				form.moblie = false;
				$(".first .contact input").css('border-color','red');
			}else{
				form.moblie = true;
				$(".first .contact input").css('border-color','#ccc');
			}
		});

		//提交基础信息
		function setFacInfo(){
			var cardf_id = $('#cardf_id').val();
			var cardb_id = $('#cardb_id').val();

			if(cardf_id == 0 || cardb_id == 0){
				noty({text:'请上传身份证【正】【反】面照片',type:'error',timeout:800});
			}else {
				$.ajax({
					type: "post",
					url: setFacInfoUrl,
					dataType: "json",
					data: $('#setFacInfo').serialize(),
					success: function (obj) {
						if (obj.level == 'success') {
							replaceTable($(".first"), 1);
						}
					},
					error: function (obj) {
						var datajson = obj.responseJSON;
						noty({text: datajson.message, type: datajson.level, timeout: 1000});
					}
				});
			}
		}
		//清除错误信息
		clearMessage(".real_name", $(".realName_error"));
		clearMessage(".contact", $(".contact_error "));
		clearMessage(".qq", $(".qq_error"));
		clearMessage(".email", $(".email_error"));
		clearMessage(".id", $(".id_error"));
		/*验证姓名等 end*/

		/*验证服务性质 -start-*/
		//隐藏公司名和简介
		$doc.on("click", "[name=type]", function() {
			var personDesc = $(".personDesc"),
				type = $(this)[0].getAttribute("data-type"),
				companyName = $(".companyName > em"),
				companyDesc = $(".companyDesc > em");

			if ($(this).hasClass("personal")) {
				$(".team .comp").hide();
				personDesc.show();
			} else {
				$(".team .comp").show();
				personDesc.hide();
			}

			if (type === "1") {
				companyName.html("工作室名字");
				companyDesc.html("工作室简介");
			}

			if (type === "2") {
				companyName.html("公司名字");
				companyDesc.html("公司简介");
			}
		});

		//提交表单
		$doc.on("click", ".team button", function() {
			var companyName = $(".companyName input").val(),
				companyDesc = $(".companyDesc textarea").val(),
				personDesc = $(".personDesc textarea").val(),
				trade = $(".trade input").val(),
				peoples = $(".peoples input").val(),
				plan = $(".plan textarea").val(),
				service = $(".service input").val();

			//是否选中个人
			if ($("[data-type=0]").prop("checked")) {
				team.type = 0;
				team.name = true; //当个人是则不显示
				team.desc = true; //当个人是则不显示
				team.peoples = true; //当个人是则不显示

				if (!utils.empty(personDesc)) {
					team.pDesc = true;
				} else {
					team.pDesc = false;
					utils.errorMessage($(".personDesc_error"), "*必填");
				}
			} else {

				if (!utils.empty(companyName)) {
					team.name = true;
				} else {
					team.name = false;
					utils.errorMessage($(".compn_error"), "*必填");
				}

				if (!utils.empty(companyDesc)) {
					team.desc = true;
				} else {
					team.desc = false;
					utils.errorMessage($(".compd_error"), "*必填");
				}

				if (!utils.empty(peoples)) {
					team.peoples = true;
				} else {
					team.peoples = false;
					utils.errorMessage($(".peoples_error"), "*必填");
				}
			}

			if (!utils.empty(trade)) {
				team.trade = true;
			} else {
				team.trade = false;
				utils.errorMessage($(".trade_error"), "*必填");
			}

			if (!utils.empty(plan)) {
				team.plan = true;
			} else {
				team.plan = false;
				utils.errorMessage($(".plan_error"), "*必填");
			}

			if (!utils.empty(service)) {
				team.service = true;
			} else {
				team.service = false;
				utils.errorMessage($(".service_error"), "*必填");
			}

			if (team.name &&
				team.desc &&
				team.trade &&
				team.peoples &&
				team.plan &&
				team.service) {

				//如果选择个人的情况下不提交 '公司名字 公司简介 公司人数'
				//do something
				var fields = [];
				$('input[name="field"]:checked').each(function () {
					fields.push($(this).val());
				});
				$('#fields').val(fields);
				var skills = [];
				$('input[name="skill"]:checked').each(function () {
					skills.push($(this).val());
				});
				$('#skills').val(skills);
				setOrgInfo()
			}
		});
		//提交公司信息
		function setOrgInfo(){
			var type = $('input[name="type"]:checked').val();
			var org_name = $('input[name="org_name"]').val();
			var org_desc = $('textarea[name="org_desc"]').html();
			var people_desc = $('textarea[name="people_desc"]').html();
			var province = $('#o_province').val();
			var city = $('#o_city').val();
			var county = $('#o_county').val();
			var experience = $('.exper').val();
			var fields = [];
			$('input[name="field"]:checked').each(function () {
				fields.push($(this).val());
			});
			var skills = [];
			$('input[name="skill"]:checked').each(function () {
				skills.push($(this).val());
			});

			var has_invoice = $('select[name="has_invoice"]').val();
			var team_num = $('input[name="team_num"]').val();
			var solution = $('textarea[name="solution"]').html();
			var other_service = $('input[name="other_service"]').val();
			var state = 1;
			$.ajax({
				type:"post",
				url:setOrgInfoUrl,
				dataType:"json",
				data:{'type':type,'people_desc':people_desc,'org_name':org_name,'org_desc':org_desc,
				'o_province':province,'o_city':city,'o_county':county,'field':fields,'skill':skills,
					'has_invoice':has_invoice,'experience':experience,'team_num':team_num,'solution':solution,'other_service':other_service,'state':state},
				success:function(obj){
					if(obj.level == 'success'){
						replaceTable($(".second"),2);
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		}
		//清除错误信息
		clearMessage(".companyName input", $(".compn_error"));
		clearMessage(".companyDesc textarea", $(".compd_error"));
		clearMessage(".trade input", $(".trade_error"));
		clearMessage(".peoples input", $(".peoples_error"));
		clearMessage(".plan textarea", $(".plan_error"));
		clearMessage(".service input", $(".service_error"));
		/*验证服务性质 -end-*/

		/*添加案例 -start-*/
		$doc.on("click", ".uploadExample .save", function() {
			var title = $(".exampleTitle input").val(),
				desc = $(".exampleDesc textarea").val(),
				link = $(".exampleLink input").val();

			if (!utils.empty(title)) {
				example.title = true;
			} else {
				example.title = false;
				utils.errorMessage($(".titleError"), "*必填");
			}

			if (!utils.empty(desc)) {
				example.desc = true;
			} else {
				example.desc = false;
				utils.errorMessage($(".descError"), "*必填");
			}


			if (!utils.empty(link)) {
				example.link = true;
			} else {
				example.link = false;
				utils.errorMessage($(".linkError"), "*必填");
			}

			if (example.title &&
				example.desc &&
				example.link) {
				setExamInfo();


				//do somthing
			}
		});
		//提交案例信息
		function setExamInfo(){
			$.ajax({
				type:"post",
				url:setExamInfoUrl,
				dataType:"json",
				data:$('#setExamInfo').serialize(),
				success:function(obj){
					if(obj.level == 'success'){
						noty({text:'提交成功',type:obj.level,timeout:500,
							callback:{
								afterClose:function(){
									$(".exampleTitle input").val("");
									$(".exampleDesc textarea").val("");
									$(".exampleLink input").val("");
									$('#target').attr('src', imgUrl);
								}
							}
						});
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		}

		//清除错误信息
		//clearErrMessage(".exampleTitle input",$(".titleError"));
		//clearErrMessage(".exampleDesc textarea", $(".descError"));
		//clearErrMessage(".exampleLink input", $(".linkError"));
		/*添加案例 -end-*/
	}();
});